package com.ilock.bean;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

/**
 * A data access object (DAO) providing persistence and search support for Iuser
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.ilock.bean.Iuser
 * @author MyEclipse Persistence Tools
 */
//@Repository
public class IuserDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(IuserDAO.class);
	// property constants
	public static final String IUSER_ACCOUNT = "iuserAccount";
	public static final String IUSER_PASSWORD = "iuserPassword";
	public static final String IUSER_PHONE_NUMBER = "iuserPhoneNumber";
	public static final String IUSER_EMAIL = "iuserEmail";
	public static final String IUSER_WECHAT = "iuserWechat";

	protected void initDao() {
		// do nothing
	}
	
//	@Resource  
//    public void setSessionFacotry(SessionFactory sessionFacotry) {  
//        super.setSessionFactory(sessionFacotry);  
//    } 

	public void save(Iuser transientInstance) {
		log.debug("saving Iuser instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Iuser persistentInstance) {
		log.debug("deleting Iuser instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Iuser findById(java.lang.Integer id) {
		log.debug("getting Iuser instance with id: " + id);
		try {
			Iuser instance = (Iuser) getHibernateTemplate().get(
					"com.ilock.bean.Iuser", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Iuser instance) {
		log.debug("finding Iuser instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public Iuser checkAccount(String email,String password){
		Iuser iuser = null;
		try {
			String queryString = "from Iuser as model where model.iuserEmail"
					+ "='"+email+"' and model.iuserPassword='"+password+"'";
			List list = getHibernateTemplate().find(queryString);
			if(list!=null){
				iuser=(Iuser) getHibernateTemplate().find(queryString).get(0);
			}	
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
		return iuser;
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Iuser instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Iuser as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByIuserAccount(Object iuserAccount) {
		return findByProperty(IUSER_ACCOUNT, iuserAccount);
	}

	public List findByIuserPassword(Object iuserPassword) {
		return findByProperty(IUSER_PASSWORD, iuserPassword);
	}

	public List findByIuserPhoneNumber(Object iuserPhoneNumber) {
		return findByProperty(IUSER_PHONE_NUMBER, iuserPhoneNumber);
	}

	public List findByIuserEmail(Object iuserEmail) {
		return findByProperty(IUSER_EMAIL, iuserEmail);
	}

	public List findByIuserWechat(Object iuserWechat) {
		return findByProperty(IUSER_WECHAT, iuserWechat);
	}

	public List findAll() {
		log.debug("finding all Iuser instances");
		try {
			String queryString = "from Iuser";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Iuser merge(Iuser detachedInstance) {
		log.debug("merging Iuser instance");
		try {
			Iuser result = (Iuser) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Iuser instance) {
		log.debug("attaching dirty Iuser instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Iuser instance) {
		log.debug("attaching clean Iuser instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IuserDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IuserDAO) ctx.getBean("IuserDAO");
	}
}